home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / et / et3_0-a1.lha / et3 / src / PathLookup.C < prev    next >
C/C++ Source or Header  |  1992-08-25  |  2KB  |  101 lines

  1. #ifdef __GNUG__
  2. #pragma implementation
  3. #endif
  4.  
  5. #include "PathLookup.h"
  6.  
  7. #include "Class.h"
  8. #include "ByteArray.h"
  9. #include "OrdColl.h"
  10. #include "System.h"
  11. #include "String.h"
  12.  
  13. //---- PathLookup ----------------------------------------------------------
  14.  
  15. NewMetaImpl(PathLookup,Object, (TP(path), TP(paths)));
  16.  
  17. PathLookup::PathLookup(char *p)
  18. {
  19.     path= strsave(p);
  20.     paths= 0;
  21.     Scan();
  22. }
  23.  
  24. PathLookup::~PathLookup()
  25. {
  26.     SafeDelete(path);
  27.     if (paths)
  28.     paths->FreeAll();
  29.     SafeDelete(paths);
  30. }
  31.  
  32. bool PathLookup::Lookup(const char *name, char *buf)
  33. {
  34.     // try current working directory
  35.     if (gSystem->AccessPathName((char*)name, 4) == 0) {
  36.     strcpy(buf, name);
  37.     return TRUE;
  38.     }
  39.     if (!paths)
  40.     return FALSE;
  41.     Iter next(paths);
  42.     ByteArray *bp;
  43.     while (bp= (ByteArray*)next()) {
  44.     sprintf(buf, "%s/%s", bp->Str(), name);
  45.     if (gSystem->AccessPathName((char*)buf, 4) == 0)
  46.         return TRUE;
  47.     }
  48.     return FALSE;    
  49. }
  50.  
  51. void PathLookup::Scan()
  52. {
  53.     char *newpath= path;
  54.  
  55.     paths= new OrdCollection(4); 
  56.     if (!newpath)
  57.     return;
  58.     char *p, *q, buf[400];
  59.     for (p= newpath, q= buf;  ; ) {
  60.     if (*p == ':' || *p == '\0') {
  61.         *q= '\0';
  62.         gSystem->ExpandPathName(buf, sizeof buf);
  63.         paths->Add(new ByteArray((byte*)buf, -1));
  64.         q= buf;
  65.         if (*p == '\0')
  66.         break;
  67.         p++;
  68.     } else
  69.         *q++= *p++;
  70.     } 
  71. }
  72.  
  73. void PathLookup::Add(char *p)
  74. {
  75.     ByteArray *b= new ByteArray((byte*)p, -1);
  76.     if (!paths->Contains(b))
  77.     paths->Add(b);
  78.     else
  79.     SafeDelete(b);
  80. }
  81.  
  82. //---- class PathIter --------------------------------------------------------
  83.  
  84. PathIter::PathIter(PathLookup *p)
  85. {
  86.     ip= p->paths->MakeIterator();    
  87. }
  88.  
  89. PathIter::~PathIter()
  90. {
  91.     SafeDelete(ip);
  92. }
  93.  
  94. char *PathIter::operator()()
  95. {
  96.     ByteArray *bp= (ByteArray*)ip->operator()();
  97.     if (bp)
  98.     return (char*) bp->Str();
  99.     return 0;
  100. }
  101.